import { routePlan, decompression, getScale } from '../../utils/userLocation';
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        mPolyline: [], // 地图连线坐标
        markerList: [], // 地图maker

        latitude: '',
        longitude: '',
        scale: 16,
		tabIndex: 0,
		
		showTypePopup: false, // 显示选择物品类型弹窗
		bottom: -730,
    },
	
	// 关闭或打开 物品类型 弹窗
	typePopup: function () {
		let showTypePopup = this.data.showTypePopup;
		console.log('showTypePopup', showTypePopup)
		let bottom = showTypePopup ? -730 : 0;
		this.setData({showTypePopup: !showTypePopup, bottom});
	},
	
	// 地图渲染完成
	fnMapUpdated: function () {
		// 地图渲染完成时 打开选择物品类型弹窗
		this.setData({showTypePopup: true, bottom: 0});
	},

    // 获取起点、终点坐标
    fnGetPoints: function () {
        // 用户的地址信息
        let user = app.get('__user__address');
        // 选择的地址信息
        let select = app.get('__address__info');
        // 选中的tab索引
        let index = app.get('__tab__index');

        let uLat = user.location.lat, 
        uLng = user.location.lng, 
        sLat = select.location.lat, 
        sLng = select.location.lng;

        // 起点、终点坐标封装
        let from = { latitude: uLat, longitude: uLng },
            to = { latitude: sLat, longitude: sLng };

        // 默认“帮我买”

        if (index === 1) { // 帮我取
            from['longitude'] = sLng;
            from['latitude'] = sLat;
            to['longitude'] = uLng;
            to['latitude'] = uLat;
        }

        this.setData({tabIndex: index, latitude: to['latitude'], longitude: to['longitude']});
        this.fnSetPolyline(from, to);
    },

    // 设置地图坐标点连线
    fnSetPolyline: function (from, to) {
        let _that = this;
        
        routePlan(from, to).then(function (data) {
            // 点位、距离、预计耗时
            const { polyline, distance, duration } = data.result.routes[0];
            // 点位解压缩
            let pointArr = decompression(polyline);
            // 封装的坐标列表
            let points = [];
            for (let i = 1; i <= pointArr.length; i += 2) {
                let point = {
                    longitude: pointArr[i],
                    latitude: pointArr[i - 1]
                }
                points.push(point);
            }
            // console.log(points);
            // console.log(pointArr);
            // 封装polyline
            let mPolyline = {
                points,
                color: '#7BC6CC',
                width: 3
            };
            // 起点、终点坐标marker
            let sMarker = _that.fnCreateMarker(points[0], '../../images/fs.png');
            let eMarker = _that.fnCreateMarker(points[points.length - 1], '../../images/js.png');
            eMarker['callout'] = _that.fnCreateCallout(`距离${(distance / 1000).toFixed(2)}km 预计${(duration / 60).toFixed(2)}小时后送达`);
            let markerList = [sMarker, eMarker];

            // 获取缩放级别
            let scale = getScale(distance);

            _that.setData({mPolyline: [mPolyline], markerList, scale});
        });
        // console.log(this.data.mPolyline)
    },

    // 创建marker
    fnCreateMarker: function (point, icon) {
        return {
            ...point,
            iconPath: icon,
            width: 24,
            height: 24,
        };
    },
    // 创建callout
    fnCreateCallout: function (content) {
        return {
            content,
            color: '#000000',
            fontSize: 14,
            borderRadius: 15,
            bgColor: '#ffffff',
            padding: 10,
            display: 'ALWAYS',
            textAlign: 'center'
        };
    },
    

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.fnGetPoints();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        // this.mapCtx = wx.createMapContext('myMap');
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})